package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.domain.DataResult;
import com.dongdongshop.em.DataResultEnum;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.service.TbGoogsService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("tbGoogsController")
public class tbGoogsController {


    @Reference
    private TbGoogsService tbGoogsService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @RequestMapping("getToshow")
    public String getToshow(){
        return "admin/goods";
    }

    @RequestMapping("selectByName")
    @ResponseBody
    public DataResult selectByName(TbGoods tbGoods){
        List<TbGoods> tbGoods1 = tbGoogsService.selectByName(tbGoods);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(tbGoods1);
    }

    @RequestMapping("updateGoods")
    @ResponseBody
    public DataResult updateGoods(@RequestParam("ids[]") Long[] ids, String auditStatus){
        try {
            tbGoogsService.updateGoods(ids,auditStatus);
            if(Objects.equals(auditStatus,"1")){
                rocketMQTemplate.convertAndSend("item-goods",ids);
            }
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

}
